﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace VASMide
{
    public partial class startForm : Form
    {
        public startForm()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Directory.CreateDirectory("projects\\" + textBox1.Text);
            Directory.CreateDirectory("projects\\" + textBox1.Text + "\\source");
            Directory.CreateDirectory("projects\\" + textBox1.Text + "\\resource");
            Directory.CreateDirectory("projects\\" + textBox1.Text + "\\bin");
            Directory.CreateDirectory("projects\\" + textBox1.Text + "\\headers");
            BinaryWriter bw = new BinaryWriter(new FileStream("projects\\" + textBox1.Text + "\\proj.dat", FileMode.Create));
            bw.Write("DS PROJ");
            bw.Write(textBox1.Text);
            string ext = "";
            foreach (Language l in Form1.languages)
            {
                if (l.name.ToLower() == listBox1.SelectedItem.ToString().ToLower())
                {
                    Project.TargetLanguage = l.name;
                    ext = l.FileExt;
                    bw.Write(l.name);
                }
            }
            StreamWriter sw = new StreamWriter("projects\\" + textBox1.Text + "\\source\\program" + ext);
            sw.Write("Insert Code here.");
            sw.Close();
            bw.Close();
            loadProject("projects\\" + textBox1.Text);
        }

        private void folderBrowserDialog1_HelpRequest(object sender, EventArgs e)
        {

        }
        
        public static void loadProject(string s)
        {
            if(!File.Exists("mru.txt"))
            {
                File.Create("mru.txt");
            }
            StreamReader sr = new StreamReader("mru.txt");
            string mrulist = sr.ReadToEnd();
            sr.Close();
            File.Delete("mru.txt");
            StreamWriter mru = new StreamWriter("mru.txt");
            mru.WriteLine(s);
            mru.WriteLine(mrulist);
            mru.Close();
            BinaryReader br = new BinaryReader(new FileStream(s + @"\proj.dat", FileMode.Open));
            br.ReadString();
            string name = br.ReadString();
            Project.TargetLanguage = br.ReadString();
            br.Close();
            foreach (string f in Directory.GetFiles(s + "\\source"))
            {
                Project.SourceFiles.Add(f);
                
            }
            foreach (string f in Directory.GetFiles(s))
            {
                if (f.EndsWith(".vcl"))
                {
                    Project.References.Add(f);
                }
            }
            foreach (string f in Directory.GetFiles(s + "\\headers"))
            {
                Project.Headers.Add(f);
            }
            Program.main.loadItems();
            Project.Path = s;
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.SelectedPath = Application.StartupPath;
            folderBrowserDialog1.ShowDialog();
            loadProject(folderBrowserDialog1.SelectedPath);this.Close();
         
        }

        private void startForm_Load(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            foreach (Language l in Form1.languages)
            {
                listBox1.Items.Add(l.name);
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (Language l in Form1.languages)
            {
                if (listBox1.SelectedItem.ToString() == l.name)
                {
                    label2.Text = l.Description;
                    if (l.icon != null)
                    {
                        pictureBox1.Image = l.icon;
                    }
                }
            }
        }
    }
}
